package com.jinghang.scala_base

/**
  * 循环表达式
  * author: zhangXu
  * createTime: 2018-12-07 20:00
  **/
object _020Loop {
  def main(args: Array[String]): Unit = {
    1 to 10 //1,2,3***,10
    1.to(10)

    println("Range(1, 10)")
    println(Range(1, 10))
    println("Range(1, 10)")
    for (elem <- Range(1, 10)) {
      println(elem)
    }



    //1到10，步长/间隔为 2
    Range(1, 10, 2)
    1 until 10
    1.until(10)

    println("to ")
    for (i <- 1 to 10) {
      println(i)
    }
    println("1 to 10  i%2 == 0")
    //条件循环
    for (i <- 1 to 10 if i % 2 == 0) {
      println(i)
    }

    for (i <- 1 to 10 ) {
      if( i % 2 == 0){
        println(i)
      }
    }



    println("Range(1, 10, 2) ")
    for (i <- Range(1, 10, 2)) {
      println(i)
    }


    println("until ")
    for (i <- 1 until 10) {
      println(i)
    }

    println("Array(\"1\", \"2\", \"3\", \"4\") ")
    val nums = Array("1", "2", "3", "4")
    for (num <- nums) {
      println(num)
    }

    // n 其实就是nums里面的每个元素
    // ==>  就是将左边的n作为参数作用上一个函数，函数开始执行
    println("nums.foreach(x => x+1)")
    nums.foreach(x => println(x + "1"))

    //把println函数作用于数组里边的每一个元素
    nums.foreach(println)
    nums.foreach(x => println(x))

    var i = 10
    while (i > 0) {
      println(i)
      //i -= 1
      i = i-1
    }



  }


}
